package cn.bearspark.api.controller.main;

import cn.bearspark.api.service.main.HappeningService;
import cn.bearspark.api.vo.main.happening.*;
import cn.bearspark.api.vo.main.user.UserSlimVO;
import cn.bearspark.common.dto.Page;
import cn.bearspark.framework.web.starter.vo.ResponseVO;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 动态模块
 *
 * @author f
 */
@Validated
@RestController
@RequestMapping("/happening")
public class HappeningController {

    private final HappeningService happeningService;

    public HappeningController(HappeningService happeningService) {
        this.happeningService = happeningService;
    }

    /**
     * 发布动态
     *
     * @param publishHappeningReqVO 发布动态的请求对象
     * @return 发布动态的响应对象
     */
    @PostMapping("/publish")
    public ResponseVO<Object> publish(@Valid @RequestBody PublishHappeningReqVO publishHappeningReqVO) {
        happeningService.publish(publishHappeningReqVO);
        return ResponseVO.success();
    }

    /**
     * 准备写动态
     *
     * @return 写动态需要的信息
     */
    @GetMapping("/prepare")
    public ResponseVO<PrepareRespVO> prepare() {
        return ResponseVO.success(happeningService.prepare());
    }

    /**
     * 分页获取动态信息
     *
     * @param pageNum        动态的页数
     * @param pageSize       动态的条数
     * @param selectedUserId 被查询动态的用户 id，可以为 null，表示查询全部动态信息
     * @return 动态的信息
     */
    @GetMapping
    public ResponseVO<Page<HappeningVO>> pageQuery(@NotNull Integer pageNum, @NotNull Integer pageSize,
                                                   Long selectedUserId) {
        return ResponseVO.success(happeningService.pageQuery(pageNum, pageSize, selectedUserId));
    }

    /**
     * 获取动态详情 TODO 增加标签的浏览量
     *
     * @param happeningId 待获取详情的动态 id
     * @return 动态详情
     */
    @GetMapping("/detail")
    public ResponseVO<HappeningDetailVO> detail(@NotNull Long happeningId) {
        return ResponseVO.success(happeningService.detail(happeningId));
    }

    /**
     * 分页查询点赞用户列表
     *
     * @param pageNum     页数
     * @param pageSize    每页的条数
     * @param happeningId 动态的 id
     * @return 点赞用户列表
     */
    @GetMapping("/list/liked")
    public ResponseVO<Page<UserSlimVO>> pageQueryLikedList(@NotNull Integer pageNum,
                                                           @NotNull Integer pageSize,
                                                           @NotNull Long happeningId) {
        return ResponseVO.success(happeningService.pageQueryLikedList(pageNum, pageSize, happeningId));
    }

    /**
     * 分页查询转发用户列表
     *
     * @param pageNum     页数
     * @param pageSize    每页的条数
     * @param happeningId 动态的 id
     * @return 转发用户列表
     */
    @GetMapping("/list/forwarded")
    public ResponseVO<Page<UserSlimVO>> pageQueryForwardedList(@NotNull Integer pageNum,
                                                               @NotNull Integer pageSize,
                                                               @NotNull Long happeningId) {
        return ResponseVO.success(happeningService.pageQueryForwardedList(pageNum, pageSize, happeningId));
    }

    /**
     * 删除动态
     *
     * @param happeningId 待删除的动态
     * @return 响应对象
     */
    @DeleteMapping
    public ResponseVO<Object> delete(@NotNull Long happeningId) {
        happeningService.delete(happeningId);
        return ResponseVO.success();
    }

    /**
     * 修改动态
     *
     * @param modifyHappeningVO 修改动态的请求对象
     * @return 响应对象
     */
    @PutMapping
    public ResponseVO<Object> modify(@Valid @RequestBody ModifyHappeningVO modifyHappeningVO) {
        happeningService.modify(modifyHappeningVO);
        return ResponseVO.success();
    }

    /**
     * 获取关注者列表
     *
     * @return 关注者列表
     */
    @GetMapping("/followerList")
    public ResponseVO<FollowerListVO> followerList() {
        return ResponseVO.success(happeningService.followerList());
    }

    /**
     * 获取表情包列表
     *
     * @return 表情包列表
     */
    @GetMapping("/emojiList")
    public ResponseVO<EmojiListVO> emojiList() {
        return ResponseVO.success(happeningService.emojiList());
    }

}
